﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.Entity;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMU.Certificates;
//using EntityFramework;


namespace CMU.Manage
{
    public partial class UCMonHoc : UserControl
    {
        CertificateEntities context = new CertificateEntities();
        public UCMonHoc()
        {
            InitializeComponent();
        }

        // LOAD
        private void UserControl6_Load(object sender, EventArgs e)
        {

            var loadmh = from mh in context.MonHoc
                         select mh;
            dataGridView1.DataSource = loadmh.ToList();
            dataGridView1.Refresh();
        }

        // ADD
        private void button2_Click(object sender, EventArgs e)
        {

            if (textBox2.Text != "" && textBox3.Text != "")
            {
                MonHoc mh = new MonHoc();
                mh.MaMH = textBox2.Text;
                mh.TenMH = textBox3.Text;

                context.MonHoc.Add(mh);
                int count = context.SaveChanges();
                if (count > 0)
                {
                    MessageBox.Show("Da them moi");
                    UserControl6_Load(sender, e);
                }
                else
                    MessageBox.Show("Da ton tai");
            }
            else
                MessageBox.Show("Hãy Nhập đầy đủ thông tin");

        }

        // DEL
        private void button6_Click(object sender, EventArgs e)
        {
            int mhID;
            if (Int32.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out mhID))
            {
                using (CertificateEntities newcontext = new CertificateEntities())
                {
                    MonHoc mh = new MonHoc() { Id = mhID };
                    newcontext.MonHoc.Attach(mh);
                    //context.ObjectStateManger.ChangeObjectState(mh, EntityState.Deleted);
                    //newcontext.Entry(mh).State = System.Data.EntityState.Deleted;
                    newcontext.MonHoc.Remove(mh);
                    
                    int count = newcontext.SaveChanges();
                    if (count > 0)
                    {
                        MessageBox.Show("Da xoa");
                        UserControl6_Load(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("Mon Hoc da duoc lien ket");
                    }
                    //string s = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                    //MonHoc mh = newcontext.MonHoc.Where(b => b.Id == int.Parse(s)).FirstOrDefault();
                    //newcontext.MonHoc.Attach(mh);
                    //newcontext.MonHoc.Remove(mh);
                    //newcontext.SaveChanges();
                    //UserControl6_Load(sender, e);
                }
            }
        }

        // EDIT
        private void button3_Click(object sender, EventArgs e)
        {
            
                MonHoc mh = new MonHoc();
                mh.MaMH = textBox2.Text;
                mh.TenMH = textBox3.Text;

                context.MonHoc.Add(mh);
                int count = context.SaveChanges();
                if (count > 0)
                {
                    MessageBox.Show("Da Sua thanh cong");
                    UserControl6_Load(sender, e);
                }
                else
                    MessageBox.Show("Da ton tai");
          
            
      
            
            //int mhID;
            //if (Int32.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out mhID))
            //{
            //    using (CertificateEntities context = new CertificateEntities())
            //    {

            //        MonHoc monhoc = new MonHoc() { Id = mhID };
            //        monhoc = (from mh in context.MonHoc
            //                  where mh.Id == mhID
            //                  select mh).FirstOrDefault<MonHoc>();

            //        textBox3.Text = monhoc.TenMH;
            //        textBox2.Text = monhoc.MaMH;

            //    }
            //}
        }




        //string s = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
        //MonHoc mh = context.MonHoc.Where(b => b.Id == int.Parse(s)).FirstOrDefault();
        //mh.MaMH = textBox2.Text;
        //mh.TenMH = textBox3.Text;
        //context.SaveChanges();
        //UserControl6_Load(sender, e);

        // Save
        private void button4_Click(object sender, EventArgs e)
        {


            MonHoc mh = new MonHoc();
            mh.MaMH = textBox2.Text;
            mh.TenMH = textBox3.Text;

            context.SaveChanges();
            int count = context.SaveChanges();
            if (count > 0)
            {
                MessageBox.Show("Da Luu Thanh Cong");
                UserControl6_Load(sender, e);
            }
            else
                MessageBox.Show("KO Luu Duoc");
        }
    }
}
   


//    public partial class EditMH : UCMonHoc
//    {
//        CertificateEntities context = new CertificateEntities();
//            int mhID;
//        MonHoc mh;
//        public EditMH (int pmhID)
//        {
//            mhID = pmhID;
//            InitializeComponent();
//        }

//private void InitializeComponent()
//{
//    throw new NotImplementedException();
